﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Logic.Packet;
using System.Net;
using System.Net.Sockets;
using Logic.Packet.Factories;
using Logic;
using GameProxy;
using PacketDotNet;
using System.Runtime.Remoting.Metadata.W3cXsd2001;


namespace PacketLogger
{
    public partial class Logger : Form, IProxyable
    {
        public PakClientFactory clientFactory { get; set; }
        public PakServerFactory serverFactory { get; set; }
        public Proxy T4CProxy { get; set; }
        private bool IsPaused;
        static int LastID;


        public Logger()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            clientFactory = new PakClientFactory();
            serverFactory = new PakServerFactory();
        }


        private void btStart_Click(object sender, EventArgs e)
        {
            if (T4CProxy == null)
            {
                Paks.Init();
                T4CProxy = new Proxy("94.23.171.50");
                T4CProxy.Attach(this);
                T4CProxy.RunWorkerAsync();
                IsPaused = false;
            }
        }


        private void btPause_Click(object sender, EventArgs e)
        {
            if (T4CProxy != null)
            {
                if (!IsPaused)
                {
                    T4CProxy.Detach(this);
                    IsPaused = true;
                    btPause.Text = "Resume";
                }
                else
                {
                    T4CProxy.Attach(this);
                    IsPaused = false;
                    btPause.Text = "Pause";
                }
            }
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            lbClient.Items.Clear();
            lbServer.Items.Clear();
        }

        private void t4CBasePakBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }


        public void Send(object sender, SendEventArgs e)
        {
            byte[] data = DecryptionRoutine.DecryptPacket(e.Data);
            T4CBasePak pak = clientFactory.Parse(data);
            LastID = pak.Id;
            lastidlabel.Text = LastID.ToString();
            lbClient.Items.Insert(0, pak.Display());
        }

        public void Receive(object sender, ReceiveEventArgs e)
        {
            byte[] data = DecryptionRoutine.DecryptPacket(e.Data);
            T4CBasePak pak = serverFactory.Parse(data);
            lbServer.Items.Insert(0, pak.Display());
        }

        private void bCraftPacket_Click(object sender, EventArgs e)
        {
            CL_PlayerMove_N crafted = new CL_PlayerMove_N(new T4CBasePak(new byte[10]));

            crafted.ActionID = "0001";
            crafted.Compute();
            crafted.Id = LastID++;
            crafted.FragmentTail = false;
            crafted.NeedAck = false;
            crafted.SecondCrypto = true;

            lock (Proxy.SendQueue)
            {
                Proxy.SendQueue.Enqueue(crafted.Data);
            }

        }
    }
}
